-- Modes normal_mode = "n",
--   insert_mode = "i",
--   visual_mode = "v",
--   visual_block_mode = "x",
--   term_mode = "t", command_mode = "c",

--  <Esc>             代表 Escape 键
--  <CR>              代表 Enter 键
--  <F1> ... <F12>    代表 功能键
--  <tab>             代表 Tab 键
--  <C-a>             代表 Ctrl  + a 键
--  <s-b>             代表 Shift + b 键
--  组合键:
--  <C-Esc>           代表 Ctrl-Esc
--  <S-F1>            代表 Shift-F1
--  <M-a>或<A-a>      代表 Alt + a
--  对于Mac用户
--  <D- >             代表Command键 super键盘。
--  > 关于键盘符号的详细说明，请使用:h key-notation命令查看帮助信息。 
-- 
-- 
--  + 不同模式下的键盘映射
--  | Command | Normal   | Visual     | Operator Pending | Insert Only | Command Line |
--  | 命令     | 常规模式  | 可视化模式   | 运算符模式        | 插入模式      | 命令行模式    |
--  |---------+----------+------------+------------------+-------------+--------------|
--  | :map    | y        | y          | y                |             |              |
--  | :nmap   | y        |            |                  |             |              |
--  | :vmap   |          | y          |                  |             |              |
--  | :omap   |          |            | y                |             |              |
--  | :map!   |          |            |                  | y           | y            |
--  | :imap   |          |            |                  | y           |              |
--  | :cmap   |          |            |                  |             | y            |


-- -- IME control with ESC
-- vim.api.nvim_set_keymap(
--     "n",
--     "<Esc>",
--     "<cmd>call system('fcitx5-remote -o > /dev/null 2>&1')<CR><Esc>",
--     { silent = true, noremap = true }
-- )
-- vim.api.nvim_set_keymap(
--     "i",
--     "<Esc>",
--     "<cmd>call system('fcitx5-remote -o > /dev/null 2>&1')<CR><Esc>",
--     { silent = true, noremap = true }
-- )

-- leader 键设置为空格                                    
vim.g.mapleader = " "    
    
-- 默认的键位设置函数太长了，所以这里将它们重新引用一下
vim.keybinds = {    
    gmap = vim.api.nvim_set_keymap,    
    bmap = vim.api.nvim_buf_set_keymap,    
    dgmap = vim.api.nvim_del_keymap,    
    dbmap = vim.api.nvim_buf_del_keymap,    
    opts = {noremap = true, silent = true}    
}    
    
-- 插入模下 jj 退出插入模式    
vim.keybinds.gmap("i", "jj", "<Esc>", vim.keybinds.opts)    
    
-- 用 H 和 L 代替 ^ 与 $    
vim.keybinds.gmap("n", "H", "^", vim.keybinds.opts)    
vim.keybinds.gmap("v", "H", "^", vim.keybinds.opts)    
vim.keybinds.gmap("n", "L", "$", vim.keybinds.opts)    
vim.keybinds.gmap("v", "L", "$", vim.keybinds.opts)    
    
-- 将 C-u 和 C-d 调整为上下滑动 10 行而不是半页    
vim.keybinds.gmap("n", "<C-u>", "10k", vim.keybinds.opts)    
vim.keybinds.gmap("n", "<C-d>", "10j", vim.keybinds.opts)    
    
-- 插入模式下的上下左右移动    
-- vim.keybinds.gmap("i", "<A-k>", "<up>", vim.keybinds.opts)    
-- vim.keybinds.gmap("i", "<A-j>", "<down>", vim.keybinds.opts)    
-- vim.keybinds.gmap("i", "<A-h>", "<left>", vim.keybinds.opts)    
-- vim.keybinds.gmap("i", "<A-l>", "<right>", vim.keybinds.opts)    
    
-- 修改分屏大小    
-- vim.keybinds.gmap("n", "<A-up>", "<cmd>res +1<CR>", vim.keybinds.opts)    
-- vim.keybinds.gmap("n", "<A-down>", "<cmd>res -1<CR>", vim.keybinds.opts)    
-- vim.keybinds.gmap("n", "<A-left>", "<cmd>vertical resize-1<CR>", vim.keybinds.opts)    
-- vim.keybinds.gmap("n", "<A-right>", "<cmd>vertical resize+1<CR>", vim.keybinds.opts)    
    
-- 正常模式下按 ESC 取消高亮显示    
vim.keybinds.gmap("n", "<ESC>", ":nohlsearch<CR>", vim.keybinds.opts)    
    
-- 通过 leader cs 切换拼写检查    
-- vim.keybinds.gmap("n", "<leader>cs", "<cmd>set spell!<CR>", vim.keybinds.opts)   

-- ToggleTerm
-- 退出终端插入模式
vim.keybinds.gmap("t", "<Esc>", "<C-\\><C-n>", vim.keybinds.opts)
-- 打开普通终端
vim.keybinds.gmap("n", "<leader>tt", "<cmd>exe v:count.'ToggleTerm'<CR>", vim.keybinds.opts)
-- 打开浮动终端
vim.keybinds.gmap("n", "<leader>tf", "<cmd>lua require('toggleterm').float_toggle()<CR>", vim.keybinds.opts)
-- 打开 lazygit 终端
vim.keybinds.gmap("n", "<leader>tg", "<cmd>lua require('toggleterm').lazygit_toggle()<CR>", vim.keybinds.opts)

-- 打开或关闭所有终端
vim.keybinds.gmap("n", "<leader>ta", "<cmd>ToggleTermToggleAll<CR>", vim.keybinds.opts)


-- bookmark's
-- vim.keybinds.gmap("n",  "ma", "<cmd>Telescope vim_bookmarks current_file<cr>", vim.keybinds.opts)
vim.keybinds.gmap("n",  "ma", "<cmd>Telescope vim_bookmarks current_file theme=ivy<cr>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "mA", "<cmd>Telescope vim_bookmarks all<cr>", vim.keybinds.opts)
vim.keybinds.gmap("n", "mA", "<cmd>Telescope vim_bookmarks all theme=ivy<cr>", vim.keybinds.opts)

-- " 设置<F3> 启动文件管理器
-- vim.keybinds.gmap("n", "<F3>", ":RnvimrToggle<cr>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fr", ":RnvimrToggle<cr>", vim.keybinds.opts)


-- Telescope
-- -- 查找文件
-- vim.keybinds.gmap("n", "<leader>ff", "<cmd>Telescope find_files theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>ff", "<cmd>Telescope find_files theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>ff", "<cmd>Telescope find_files theme=ivy<CR>", vim.keybinds.opts)
-- -- fd 查找文件 fd
vim.keybinds.gmap("n", "<leader>fd", "<cmd>Telescope fd<CR>", vim.keybinds.opts)
-- 文件管理
vim.keybinds.gmap("n", "<leader>fm", "<cmd>Telescope file_browser theme=ivy<CR>", vim.keybinds.opts)

-- -- 查找文字
-- vim.keybinds.gmap("n", "<leader>fg", "<cmd>Telescope live_grep theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fg", "<cmd>Telescope live_grep theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>fg", "<cmd>Telescope live_grep theme=ivy<CR>", vim.keybinds.opts)
-- -- 查找特殊符号
-- vim.keybinds.gmap("n", "<leader>fb", "<cmd>Telescope buffers theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fb", "<cmd>Telescope buffers theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>fb", "<cmd>Telescope buffers theme=ivy<CR>", vim.keybinds.opts)
-- -- 查找帮助文档
-- vim.keybinds.gmap("n", "<leader>fh", "<cmd>Telescope help_tags theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fh", "<cmd>Telescope help_tags theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>fh", "<cmd>Telescope help_tags theme=ivy<CR>", vim.keybinds.opts)
-- -- 查找最近打开的文件
-- vim.keybinds.gmap("n", "<leader>fo", "<cmd>Telescope oldfiles theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fo", "<cmd>Telescope oldfiles theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>fo", "<cmd>Telescope oldfiles theme=ivy<CR>", vim.keybinds.opts)
-- -- 查找 marks 标记
-- vim.keybinds.gmap("n", "<leader>fm", "<cmd>Telescope marks theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fm", "<cmd>Telescope marks theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>mn", "<cmd>Telescope marks theme=ivy<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>mc", "<cmd>Telescope vim_bookmarks current_file theme=ivy<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>ma", "<cmd>Telescope vim_bookmarks all theme=ivy<CR>", vim.keybinds.opts)
-- -- 查找按键
vim.keybinds.gmap("n", "<leader>tk", "<cmd>Telescope keymaps theme=ivy<CR>", vim.keybinds.opts)

-- -- 项目
-- vim.keybinds.gmap("n", "<leader>fp", "<cmd>Telescope project theme=dropdown<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>fp", "<cmd>Telescope project theme=cursor<CR>", vim.keybinds.opts)
vim.keybinds.gmap("n", "<leader>fp", "<cmd>Telescope project theme=ivy<CR>", vim.keybinds.opts)


-- Nvim-tree
vim.keybinds.gmap("n", "<leader>1", "<cmd>NvimTreeToggle<CR>", vim.keybinds.opts)
-- -- 按 leader f e 打开文件树
vim.keybinds.gmap("n", "<leader>fe", "<cmd>NvimTreeToggle<CR>", vim.keybinds.opts)
-- -- 按 leader f 在文件树中找到当前以打开文件的位置
vim.keybinds.gmap("n", "<leader>ft", "<cmd>NvimTreeFindFile<CR>", vim.keybinds.opts)


-- -- 打开Ranger
-- vim.keybinds.gmap("n", "<leader>fr", ":Ranger<CR>", vim.keybinds.opts)
-- vim.keybinds.gmap("n", "<leader>r", ":Ranger<CR>", vim.keybinds.opts)


-- Which-key
local whichkey = require("which-key")
-- whichkey.register(
--     {
--         d = { "<Cmd>Telescope lsp_definitions<CR>", "Definitions" },
--         t = { "<Cmd>Telescope lsp_type_definitions<CR>", "Type definitions" },
--         I = { "<Cmd>Telescope lsp_implementations<CR>", "Goto implementation" },
--         r = { "<Cmd>Telescope lsp_references<CR>", "References" },
--         ["@"] = { "<Cmd>Telescope lsp_document_symbols<CR>", "Document Symbols" },
--         w = { "<Cmd>Telescope lsp_document_symbols<CR>", "Workspace Document Symbols" },
--         ["."] = { "<Cmd>Telescope lsp_code_actions<CR>", "Code Action" },
--         [","] = { "<Cmd>Telescope lsp_range_code_actions<CR>", "Range Code Action" },
--     }, { 
--         prefix = "g" 
--     }
-- )
whichkey.register(
    {
        t = {
            name = "Telescope",
            b = { "<Cmd>Telescope buffers<CR>", "List buffers" },
            B = { "<Cmd>Telescope builtin<CR>", "Find builtin features" },
            f = { "<Cmd>Telescope find_files<CR>", "Find file" },
            g = { "<Cmd>Telescope live_grep<CR>", "Live grep" },
            G = { "<Cmd>Telescope git_files<CR>", "Git files" },
            r = { "<Cmd>Telescope oldfiles<CR>", "MRU" },
            k = { "<Cmd>Telescope keymaps<CR>", "Keymap" },
        },
    }, { 
        -- prefix = "<Leader>" 
        prefix = "t" 
    }
)



-- 注释
-- 测试按键
-- vim.keybinds.gmap("n", "<leader>ss", "<cmd>echo 'hello'<CR>", vim.keybinds.opts)   

